﻿

//生成随机字符串
function randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

//源于C#中的string.Format()
String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题，谢谢何以笙箫的指出
                    var reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}



$.extend({
    httphelp:{
        appid:"4d53bce03ec34c0a911182d4c228ee6c",
        secretKey:"A93reRTUJHsCuQSHR+L3GxqOJyDmQpCgps102ciuabc=",
        token :"",
        GetTimeStamp:function () {
            return Date.parse(new Date()) / 1000;
        },
        GetRandom:function (len) {
            len = len || 32;
            var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
            var maxPos = $chars.length;
            var pwd = '';
            for (i = 0; i < len; i++) {
                pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
            }
            return pwd;
        },
        GetSignature:function (HttpMethod, url, content) {
            var template = "{0}{1}{2}{3}{4}{5}{6}";
            var requestContentBase64String = "";
            if (typeof (content) != "undefined") {
                requestContentBase64String = b64_md5($.param(content)) + "==";
            }
            var requestUri = encodeURIComponent(url).toLowerCase();
            var timestamp = this.GetTimeStamp();
            var nonce = this.GetRandom(8);
            var data = template.format(this.appid, HttpMethod, requestUri, timestamp, nonce, requestContentBase64String, this.token);
            var hash = new jsSHA("SHA-256", "TEXT");
            hash.setHMACKey(secretKey, "B64");
            hash.update(data);
            var Signature = hash.getHMAC("B64");
            return Signature;
        },
        GetHeaderStr:function (HttpMethod, url, content) {
            var template = "{0}{1}{2}{3}{4}{5}{6}";
            var requestContentBase64String = "";
            if (typeof (content) != "undefined") {
                requestContentBase64String = b64_md5($.param(content)) + "==";
            }
            var requestUri = encodeURIComponent(url).toLowerCase();
            var timestamp = this.GetTimeStamp();
            var nonce = this.GetRandom(8);
            var data = template.format(this.appid, HttpMethod, requestUri, timestamp, nonce, requestContentBase64String, this.token);
            var hash = new jsSHA("SHA-256", "TEXT");
            hash.setHMACKey(this.secretKey, "B64");
            hash.update(data);
            var Signature = hash.getHMAC("B64");
            return 'amx ' + this.appid + ":" + Signature + ":" + this.token + ":" + nonce + ":" + timestamp;
        },
        authError:function(result){
        	if(result.code!=0){
	        	alert(result.message);
	        	window.location.href='../login/login.html';
        	}
        },
        error:function(result){
			var r=eval('('+result.responseText+')');
			alert(r.message);
        }
    }
});






//签名包生成
function getincomingBase64Signature(appid, HttpMethod, url, token, nonce, timestamp, content) {
    var template = "{0}{1}{2}{3}{4}{5}{6}";
    var requestContentBase64String = "";
    if (typeof (content) != "undefined")
    {
        requestContentBase64String = b64_md5($.param(content))+"==";
    }
    var requestUri = encodeURIComponent(url).toLowerCase();
    var data = template.format(appid, HttpMethod, requestUri, timestamp, nonce, requestContentBase64String, token);
    var hash = new jsSHA("SHA-256", "TEXT");
    hash.setHMACKey(secretKey, "B64");
    hash.update(data);
    var incomingBase64Signature = hash.getHMAC("B64");
    return incomingBase64Signature;
}

//URL参数转成json
$.par2Json = function (string, overwrite) {
    var obj = {},
    pairs = string.split('&'),
    d = decodeURIComponent,
    name,
    value;
    $.each(pairs, function (i, pair) {
        pair = pair.split('=');
        name = d(pair[0]);
        value = d(pair[1]);
        obj[name] = overwrite || !obj[name] ? value :
        [].concat(obj[name]).concat(value);
    });
    return obj;
};

//json转成url参数
$.json2Par = function (o, pre) {
    var undef, buf = [], key, e = encodeURIComponent;
    for (key in o) {
        undef = o[key] == 'undefined';
        $.each(undef ? key : o[key], function (val, i) {
            buf.push("&", e(key), "=", (val != key || !undef) ? e(val) : "");
        });
    }
    if (!pre) {
        buf.shift();
        pre = "";
    }
    return pre + buf.join('');
};